//
//  MainViewModel.swift
//  GomroClient
//
//  Created by mac on 2020/4/10.
//  Copyright © 2020 cn.gomro.app. All rights reserved.
//

import Foundation

/** 首页ViewModel */
class MainViewModel: BaseViewModel {
    
    
    //MARK: - 分时数据
    private func loadFSData(isLoading:Bool = true,
                            andRACSubscriber subscriber:RACSubscriber? = nil){
        let _path = Bundle.main.path(forScaledResource: "USD_fenshi", ofType: "json")
        let _data:Data = try! NSData.init(contentsOfFile: _path!) as Data
        let jsonData = try? JSONSerialization.jsonObject(with: _data, options: [])
        
        subscriber?.sendNext(jsonData)
        subscriber?.sendCompleted()
    }
    
    
    //MARK: - 5分钟K线数据
    private func load5minData(isLoading:Bool = true,
                              andRACSubscriber subscriber:RACSubscriber? = nil){
        let _path = Bundle.main.path(forScaledResource: "USD_min5", ofType: "json")
        let _data:Data = try! NSData.init(contentsOfFile: _path!) as Data
        let jsonData = try? JSONSerialization.jsonObject(with: _data, options: [])
        
        subscriber?.sendNext(jsonData)
        subscriber?.sendCompleted()
    }
    
    
    //MARK: - lazy load
    /** 顶部菜单点击事件 */
    lazy var topMenuCommand:RACCommand = {
        let _com = RACCommand.init {[weak self] (input:Any?) -> RACSignal? in
            guard let self = self else { return nil }
            
            let signal:RACSignal = RACSignal.createSignal {(subscriber:RACSubscriber?) -> RACDisposable? in
                subscriber?.sendNext(input)
                subscriber?.sendCompleted()
                return nil
            }
            
            return signal
        }
        
        return _com!
    }()
    
    /** 分时数据加载 */
    lazy var loadFSDataCommand:RACCommand = {
        
        let _com = RACCommand.init {[weak self] (input:Any?) -> RACSignal? in
            guard let self = self else { return nil }
            
            let signal:RACSignal = RACSignal.createSignal {[weak self] (subscriber:RACSubscriber?) -> RACDisposable? in
                guard let self = self else { return nil }
                guard let _loading = input as? Bool else { return nil }
                
                self.loadFSData(isLoading:_loading,andRACSubscriber:subscriber)
                return nil
            }
            
            return signal
        }
        
        return _com!
    }()
    
    /** 5分钟K线 */
    lazy var loadMin5DataCommand:RACCommand = {
        
        let _com = RACCommand.init {[weak self] (input:Any?) -> RACSignal? in
            guard let self = self else { return nil }
            
            let signal:RACSignal = RACSignal.createSignal {[weak self] (subscriber:RACSubscriber?) -> RACDisposable? in
                guard let self = self else { return nil }
                guard let _loading = input as? Bool else { return nil }
                
                self.load5minData(isLoading:_loading,andRACSubscriber:subscriber)
                return nil
            }
            
            return signal
        }
        
        return _com!
    }()
}
